
//引入持久层
let { addInsures, getInsures, deleteInsures, updateInsures, searchInsures } = require('../dao/insuresDao');
const { moveFiles, deleteFiles } = require('../utils/handleFiles');

//获取所有的保险产品
module.exports.getInsures = async function (result) {
    let data = await getInsures(result);
    if (data) {
        return {
            code: 1,
            msg: '获取成功',
            data
        }
    }
    else {
        return {
            code: 0,
            msg: '获取失败',
        }

    }
}

//搜索
module.exports.searchInsures = async function (data) {
    let result = await searchInsures(data)
    if (result) {
        return {
            code: 1,
            msg: '获取成功',
            result
        }
    }
    else {
        return {
            code: 0,
            msg: '获取失败',
        }

    }
}

//新增
module.exports.addInsures = async function (insuresInfo) {
    let data = await addInsures(insuresInfo)
    if (data) {
        if (data.filename) {
            moveFiles({
                fromPath: './public/img',
                toPath: './public/images',
                filename: data.filename
            });
            deleteFiles("./public/img");
            return {
                code: 1,
                msg: '新增成功',
                data
            }
        }
    }

}

//删除
module.exports.deleteInsures = async function (delID) {
    let data = await deleteInsures(delID)
    if (data.deletedCount) {
        return {
            code: 1,
            msg: '删除成功',
            data
        }
    } else {
        return {
            code: 0,
            msg: '删除失败',
        }
    }
}

//修改
module.exports.updateInsures = async function (updateInfo) {
    let data = await updateInsures(updateInfo);
    if (data) {
        return {
            code: 1,
            msg: '修改成功',
            data
        }
    }
    else {
        return {
            code: 0,
            msg: '修改失败',
        }
    }
}
